local Entity = require'entities.entity'

local Actor = {}
Actor.mt = { __index = Actor }
setmetatable(Actor, Entity.mt )

function Actor.initialize(type, health, deck )	
	
	
	local dt, tt
	
	if deck then dt = deck else dt = 'base_actor' end
	if type then tt = type else tt = 'actor' end
	
	local act = Entity.new( tt, dt )
	setmetatable(act, Actor.mt)
	
	
	
	--local map = MapManager:get_current_map()

	act.health = health or 100
	act.travel_speed = 5
	act.move_queue = {}
	
	--act.path = MOAIPathFinder.new()
	--act.path_graph = MOAIGridPathGraph.new()
	--self.setLoc = function(x) end
	--self.set_col_grid = function(x) end
	
	act.type = type or "actor"
	
	act.passenger_id = 0
	local ba = false
	act.prop:setPriority(100)
	
	
	EntityManager:add_actor( act )
	--print("new actor")
	
	return act

end

function Actor.new( type, health, deck)

	local o = Actor.initialize(type, health, deck)
	
	return o

end

function Actor.board_ship( self, ship )
	print("ACTOR "..self.type.." boarding ship ".. ship.ship_id)
	ship_id = ship.ship_id
	if ship then
		local p_id = rawget(self, ship_id)
		
		if p_id then
			print("ACTOR BOARD p_id ".. p_id)
			if p_id > 0 then
				self.depart_ship( )
			end
		end
		self.ship_id = ship_id
		self:setAttrLink_t( MOAITransform.INHERIT_TRANSFORM, ship, MOAITransform.TRANSFORM_TRAIT )
		
		ship:add_passenger(self)
		
		self.col_grid = self:set_col_grid( ship.col_grid )
		
		self.path = jPath(self.col_grid, 'JPS', WALKABLE_TILE)
		
		self.prop:setPriority( 1000 )
		ship.layer:insertProp(self.prop)
		
			
	end

end

function Actor:depart_ship( )
	
	if self.ship_id > 0 then
		local shizzup = ConstructManager.ships[self.ship_id]
		if shizzup then
			shizzup:remove_passenger( self )
			shizzup.layer:removeProp(self.prop)
			self.clearAttrLink( MOAITransform.INHERIT_TRANSFORM )			
			table.remove(shizzup.passengers, self.passenger_id)
			
			
		end
	end


end


function Actor:path_to_loc(x, y)

	local pather = function(self, x, y)
		print("PATHER pathing..")
		local startx, starty = Entity.getLocalTile(self)
		local endx, endy = x, y
		
		if not self.move_queue then self.move_queue = {} end
		
		
		
		local path, length = self.path:getPath(startx, starty, endx, endy)
		if path then
			print(('PATHING Path found! Length: %.2f'):format(length))
			for node, count in path:iter() do
				local iter = #self.move_queue + 1
				print(('PATHING Step: %d - x: %d - y: %d'):format(count, node.x, node.y))
				if not self.move_queue[iter] then self.move_queue[iter] = {} end
				self.move_queue[iter].x = node.x
				self.move_queue[iter].y = node.y
				
			end
		else
			print("PATHING no path")
		end
	
		
	
		--[[
		local x2, y2 = self:getLoc()
		local x3, y3 = self:getWorldLoc()
		
		print( 'PATHER path to ' .. x .. ' ' .. y )
		print( 'PATHER actor at ' .. x2 .. ' ' .. y2 )
		print( 'PATHER world actor at ' .. x3 .. ' ' .. y3 )
		
		local x4, y4 = self.col_grid:locToCoord( x3, y3)
		local addr = self.col_grid:locToCellAddr( x3, y3)
		
		print( 'PATHER locToCoord ' .. x4 .. ' ' .. y4)
		print( 'PATHER cell addr ' .. addr)
		
		local start_addr = addr
		local end_addr = self.col_grid:getCellAddr(x, y)
		print( 'PATHER end_addr ' .. end_addr)
		
		self.path:init( start_addr, end_addr )
		
		while self.path:findPath( ) do
		
			print('PATHER finding path..')
			
		
		end
		
		path_size = self.path:getPathSize()
		
		for i = 1, path_size do
			local entry = self.path:getPathEntry( i )
			local x, y = self.grid:cellAddrToCoord( entry )
			self:seekLoc(x, y, 0.1)
		
		end
		
		]]
	
	end
	
	local thread = MOAICoroutine.new()
	thread:run( pather, self, x, y)
	

end

function Actor:follow_path()

	local pathfol = function(self)

		if #self.move_queue > 0 then
	
			self.prop:seekLoc( self.move_queue[1].x*TILE_SIZE, self.move_queue[1].y*TILE_SIZE, self.travel_speed)
			table.remove(self.move_queue, 1)
	
		end
	end
	
	local thread = MOAICoroutine.new()
	thread:run( pathfol, self)

end

function Actor.update(dt)

	local act_upd = function()
		local actors = EntityManager:get_actors()

		for i = 1, #actors do
		
			actors[i]:upd(dt)
			
			
		end
	end
	
	local a = MOAICoroutine.new()
	a:run(act_upd)

end

function Actor:upd(dt)

	self:follow_path()

end



function Actor:set_col_grid(grid)
	
	local col_grid
	
	if not grid then
		local ship = self:get_ship()
		
		col_grid = ship.col_grid
	else
		col_grid = grid
		
	end
	
	
	return col_grid
	
end

--[[

function Actor:set_col_grid( grid )

	if not grid then
		self.col_grid = MapManager:get_current_col_grid()
	else
		self.col_grid = grid
	end

end
]]

return Actor